package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParam;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController{
    @Autowired
    private StudentService studentService;

    //用于学员列表数据的条件分页查询
    @GetMapping
    public Result list(StudentQueryParam studentQueryParam) {
        log.info("查询学员列表，参数：{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.list(studentQueryParam);
        return Result.success(pageResult);
    }

    //添加学员信息
    @Log
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("添加学员，参数：{}", student);
        studentService.add(student);
        return Result.success();
    }

    //根据学员ID查询学员信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学员信息：{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    //修改学员信息
    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息：{}", student);
        studentService.update(student);
        return Result.success();
    }

    //批量删除学员信息
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除学员信息：{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    //违纪处理  违纪处理一次，需要将违纪次数+1，违纪扣分+前端输入的分数。
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理，学员ID：{}，违纪分数：{}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }

}
